package com.kl.oss.tencent;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.utils.Md5Utils;
import lombok.Data;

import java.io.*;
import java.util.Objects;

/**
 * @author kl liu
 * @description
 * @date 2021/9/3 10:44 上午
 */
@Data
public class TencentCosManager {
    private String secretId;
    private String secretKey;
    private String region;
    private COSClient cosClient;

    public static TencentCosManager newTencentCosManager(String secretId, String secretKey, String region) {
        TencentCosManager cosManager = new TencentCosManager();
        cosManager.setSecretId(secretId);
        cosManager.setSecretKey(secretKey);
        cosManager.setRegion(region);
        return cosManager;
    }

    /**
     * 构造函数之后调用
     */
    public void init() {
        COSCredentials cred = new BasicCOSCredentials(this.secretId, this.secretKey);
        Region reg = new Region(this.region);
        ClientConfig clientConfig = new ClientConfig(reg);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        this.cosClient = new COSClient(cred, clientConfig);
    }

    /**
     * 关闭客户端连接
     */
    public void shutdown() {
        if (!Objects.isNull(this.cosClient)) {
            this.cosClient.shutdown();
        }
    }

    /**
     * 上传文件
     */
    public PutObjectResult putObject(CosUploadReq uploadReq) {
        Objects.requireNonNull(uploadReq);
        Objects.requireNonNull(uploadReq.getBucketName());
        Objects.requireNonNull(uploadReq.getContent());
        Objects.requireNonNull(uploadReq.getStoreName());
        try (ByteArrayInputStream stream = new ByteArrayInputStream(uploadReq.getContent())) {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(uploadReq.getContent().length);
            String contentMd5 = Md5Utils.md5AsBase64(uploadReq.getContent());
            metadata.setContentMD5(contentMd5);
            if (Objects.nonNull(uploadReq.getContentType())) {
                metadata.setContentType(uploadReq.getContentType());
            }
            return cosClient.putObject(uploadReq.getBucketName(), uploadReq.getStoreName(), stream, metadata);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 下载文件
     */
    public Object downLoadFile(String bucketName, String key) {
        File downFile = new File("temp");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        cosClient.getObject(getObjectRequest, downFile);
        try (FileInputStream fos = new FileInputStream(downFile);
             ObjectInputStream ois = new ObjectInputStream(fos)) {
            if (downFile.exists()) {
                downFile.delete();
            }
            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
